package com.lqm.mall.product.app;

import com.lqm.common.utils.PageUtils;
import com.lqm.common.utils.R;
import com.lqm.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lqm.mall.product.entity.AttrEntity;
import com.lqm.mall.product.entity.AttrGroupEntity;
import com.lqm.mall.product.entity.CategoryEntity;
import com.lqm.mall.product.service.AttrAttrgroupRelationService;
import com.lqm.mall.product.service.AttrGroupService;
import com.lqm.mall.product.service.AttrService;
import com.lqm.mall.product.service.CategoryService;
import com.lqm.mall.product.vo.AttrGroupRelationVo;
import com.lqm.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author luqimei
 * @email 2274992309@qq.com
 * @date 2024-12-19 21:23:19
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if(attrGroup!=null){
            List<Long> path=new ArrayList<>();
            getAttrGroupPath(attrGroup.getCatelogId(),path);
            Long[] attrGroupPath=new Long[path.size()];
            int len=path.size();
            for(int i=0;i<len;++i){
                attrGroupPath[i]=path.get(len-1-i);
            }
            attrGroup.setCatelogPath(attrGroupPath);
        }
        return R.ok().put("attrGroup", attrGroup);
    }

    private void getAttrGroupPath(Long catelogId,List<Long> path) {
        if(catelogId==null){
            return;
        }
        path.add(catelogId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        Long parentCid = categoryEntity.getParentCid();
        if(parentCid==null || parentCid==0){
            return;
        }
        getAttrGroupPath(parentCid,path);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 查询关联
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> res=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",res);
    }

    /**
     * 查询未关联
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params){
        PageUtils res=attrService.getRelationNoAttr(attrgroupId,params);
        return R.ok().put("page",res);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntity){
        attrGroupService.deleteRelation(relationEntity);
        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }

    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }


}
